//
//  CMDBModifyUserManageViewController.swift
//  CMDB
//
//  Created by 王磊 on 25/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

private var CMDB_Modify_Margin: CGFloat = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5

class CMDBModifyUserManageViewController: NormalTitleViewController {
    
    private var nameLabel: UILabel = UILabel()
    
    private var tf: UITextField = UITextField()
    
    private var relationLabel: UILabel = UILabel()
    
    private var confirmBtn: UIButton = UIButton(type: .Custom)
    
    private var assistantBtn: UIButton = UIButton(type: .Custom)
    
    private var brokerBtn: UIButton = UIButton(type: .Custom)
    
    internal var editAble: Bool = true {
        willSet {
            
            tf.userInteractionEnabled = newValue
        }
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        if editAble {
            
            tf.becomeFirstResponder()
            
        }
    }
    override func setup() {
        super.setup()
        
        buildSubviews()
        
        buildConfirmBtnView()
    }
}
extension CMDBModifyUserManageViewController
{
    private func buildSubviews() {
        
        nameLabel.backgroundColor = UIColor.whiteColor()
        
        contentView!.backgroundColor = UIColor.Hex(0xE2E2E2)
        
        contentView!.addSubview(nameLabel)
        
        nameLabel.textAlignment = .Left
        
        nameLabel.font = CMDB_15_FONT
        
        nameLabel.textColor = word87Color
        
        contentView!.addSubview(tf)
        
        tf.font = CMDB_15_FONT
        
        tf.attributedPlaceholder = NSAttributedString(string: "请输入真实姓名", attributes: [NSFontAttributeName: CMDB_15_FONT,NSForegroundColorAttributeName: word38Color])
        
        nameLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(0)
            
            make.top.equalTo(10)
            
            make.height.equalTo(44)
            
            make.right.equalTo(0)
        }
        
        nameLabel.text = "    姓名:    "
        
        var leftMargin = nameLabel.text!.suitToSize(CGSizeMake(999, 999), fontSize: 15, lineSpacing: 3).width
        
        tf.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(leftMargin)
            
            make.centerY.equalTo(self!.nameLabel.snp_centerY)
            
        }
        
        contentView!.addSubview(relationLabel)
        
        relationLabel.textAlignment = .Left
        
        relationLabel.font = CMDB_15_FONT
        
        relationLabel.textColor = word87Color
        
        relationLabel.text = "    选择用户关系:    "
        
        relationLabel.backgroundColor = UIColor.whiteColor()
        
        relationLabel.snp_makeConstraints { [weak self]  (make) in
            
            make.left.equalTo(0)
            
            make.top.equalTo(self!.nameLabel.snp_bottom).offset(10)
            
            make.height.equalTo(64)
            
            make.right.equalTo(0)
        }
        
        assistantBtn.setTitle("助理", forState: .Normal)
        
        assistantBtn.setTitle("助理", forState: .Selected)
        
        assistantBtn.setTitleColor(UIColor.Hex6(0xff6d00, alpha: 54), forState: .Selected)
        
        assistantBtn.setTitleColor(word38Color, forState: .Normal)
        
        brokerBtn.setTitle("经纪人", forState: .Normal)
        
        brokerBtn.setTitle("经纪人", forState: .Selected)
        
        brokerBtn.setTitleColor(UIColor.Hex6(0xff6d00, alpha: 54), forState: .Selected)
        
        brokerBtn.setTitleColor(word38Color, forState: .Normal)
        
        contentView!.addSubview(assistantBtn)
        
        contentView!.addSubview(brokerBtn)
        
        leftMargin = relationLabel.text!.suitToSize(CGSizeMake(999, 999), fontSize: 15, lineSpacing: 3).width
        
        assistantBtn.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(leftMargin)
            
            make.width.equalTo(64)
            
            make.height.equalTo(32)
            
            make.centerY.equalTo(self!.relationLabel.snp_centerY)
        }
        
        assistantBtn.titleLabel?.font = CMDB_15_FONT
        
        brokerBtn.titleLabel?.font = CMDB_15_FONT
        
        leftMargin = "    ".suitToSize(CGSizeMake(999, 999), fontSize: 15, lineSpacing: 3).width
        
        brokerBtn.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(self!.assistantBtn.snp_right).offset(leftMargin)
            
            make.width.equalTo(64)
            
            make.height.equalTo(32)
            
            make.centerY.equalTo(self!.relationLabel.snp_centerY)
        }
        
        assistantBtn.layer.cornerRadius = 5
        
        assistantBtn.layer.borderWidth = 1
        
        brokerBtn.layer.cornerRadius = 5
        
        brokerBtn.layer.borderWidth = 1
        
        assistantBtn.addTarget(self, action: #selector(assistantBtnTriggered), forControlEvents: .TouchUpInside)
        
        brokerBtn.addTarget(self, action: #selector(brokerBtnTriggered), forControlEvents: .TouchUpInside)
        
        configItems()
        //        assistantBtnTriggered(assistantBtn)
    }
    
    @objc private func assistantBtnTriggered(sender: UIButton) {
        
        assistantBtn.selected = true
        
        brokerBtn.selected = false
        
        brokerBtn.layer.borderColor = word12Color.CGColor
        
        assistantBtn.layer.borderColor = UIColor.Hex6(0xff6d00, alpha: 54).CGColor
        
    }
    
    @objc private func brokerBtnTriggered(sender: UIButton) {
        
        
        assistantBtn.selected = false
        
        brokerBtn.selected = true
        
        brokerBtn.layer.borderColor = UIColor.Hex6(0xff6d00, alpha: 54).CGColor
        
        assistantBtn.layer.borderColor = word12Color.CGColor
        
        
    }
    private func configItems() {
        
        assistantBtn.selected = true
        
        brokerBtn.selected = false
        
        brokerBtn.layer.borderColor = word12Color.CGColor
        
        assistantBtn.layer.borderColor = UIColor.Hex6(0xff6d00, alpha: 54).CGColor
        
    }
    
    private func buildConfirmBtnView() {
        
        contentView!.addSubview(confirmBtn)
        
        confirmBtn.setTitle("确定", forState: .Normal)
        confirmBtn.setTitle("确定", forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff, alpha: 0.54), forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff), forState: .Normal)
        
        confirmBtn.backgroundColor = UIColor.Hex6(0xff6d00, alpha: 54)
        
        confirmBtn.titleLabel!.font = CMDB_17_FONT
        
        confirmBtn.addTarget(self, action: #selector(confirmTriggrred), forControlEvents: .TouchUpInside)
        
        confirmBtn.layer.cornerRadius = 5
        
        confirmBtn.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(CMDB_Modify_Margin)
            
            make.right.equalTo(-CMDB_Modify_Margin)
            
            make.height.equalTo(88 * CMDB_Rate)
            
            make.top.equalTo(self!.relationLabel.snp_bottom).offset(40)
        }
    }
}
extension CMDBModifyUserManageViewController {
    
    @objc private func confirmTriggrred() {
        
        let user = CMDBUserInfoBean()
        
        user.name = tf.text ?? ""
        
        user.icon = ""
        
        dataBlock!(nil,user,nil)
        
        navigationController?.popViewControllerAnimated(true)
    }
}
